load("@com_github_ash2k_bazel_tools//multirun:def.bzl", "command", "multirun")

sh_test(
    name = "test_command_env",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :command_env1)",
        "0",
        "$(location expected_command_env.txt)",
    ],
    data = [
        "expected_command_env.txt",
        ":command_env1",
    ],
)

command(
    name = "command_env1",
    command = ":command_env2",
    environment = {
        "A": "a",
    },
)

command(
    name = "command_env2",
    command = "echo_env.sh",
    environment = {
        "B": "b",
    },
)

sh_test(
    name = "test_multirun_success",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :multirun_success)",
        "0",
        "$(location expected_success.txt)",
    ],
    data = [
        "expected_success.txt",
        ":multirun_success",
    ],
)

sh_test(
    name = "test_multirun_failure",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :multirun_failure)",
        "5",
        "$(location expected_failure.txt)",
    ],
    data = [
        "expected_failure.txt",
        ":multirun_failure",
    ],
)

sh_test(
    name = "test_multirun_failure_no_stop",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :multirun_failure_no_stop)",
        "5",
        "$(location expected_failure_no_stop.txt)",
    ],
    data = [
        "expected_failure_no_stop.txt",
        ":multirun_failure_no_stop",
    ],
)

sh_test(
    name = "test_multirun_parallel_success",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        # Sort the output for deterministic comparison; order doesn't matter as long as all the expected lines are
        # there.
        "'$(location :multirun_parallel_success) | sort'",
        "0",
        "$(location expected_parallel_success.txt)",
    ],
    data = [
        "expected_parallel_success.txt",
        ":multirun_parallel_success",
    ],
)

sh_test(
    name = "test_multirun_no_tag",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        # Sort the output for deterministic comparison; order doesn't matter as long as all the expected lines are
        # there.
        "'$(location :multirun_parallel_no_tag) | sort'",
        "0",
        "$(location expected_parallel_no_tag.txt)",
    ],
    data = [
        "expected_parallel_no_tag.txt",
        ":multirun_parallel_no_tag",
    ],
)

sh_test(
    name = "test_multirun_parallel_tagged_success",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        # Sort the output for deterministic comparison; order doesn't matter as long as all the expected lines are
        # there.
        "'$(location :multirun_parallel_tagged_success) | sort'",
        "0",
        "$(location expected_parallel_tagged_success.txt)",
    ],
    data = [
        "expected_parallel_tagged_success.txt",
        ":multirun_parallel_tagged_success",
    ],
)

sh_test(
    name = "test_multirun_parallel_failure",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        # Sort the output for deterministic comparison; order doesn't matter as long as all the expected lines are
        # there.
        "'$(location :multirun_parallel_failure) | sort'",
        "5",
        "$(location expected_parallel_failure.txt)",
    ],
    data = [
        "expected_parallel_failure.txt",
        ":multirun_parallel_failure",
    ],
)

sh_test(
    name = "test_multirun_parallel_failure_no_stop",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        # Sort the output for deterministic comparison; order doesn't matter as long as all the expected lines are
        # there.
        "'$(location :multirun_parallel_failure_no_stop) | sort'",
        "5",
        "$(location expected_parallel_failure_no_stop.txt)",
    ],
    data = [
        "expected_parallel_failure_no_stop.txt",
        ":multirun_parallel_failure_no_stop",
    ],
)

sh_binary(
    name = "exit",
    srcs = ["exit.sh"],
)

sh_binary(
    name = "echo",
    srcs = ["echo.sh"],
)

multirun(
    name = "multirun_success",
    commands = [
        ":command_echo_1",
        ":command_echo_2",
    ],
)

multirun(
    name = "multirun_parallel_success",
    commands = [
        ":command_echo_2",
        ":command_echo_1",
    ],
    jobs = 0,
)

multirun(
    name = "multirun_parallel_no_tag",
    add_tag = False,
    commands = [
        ":command_echo_2",
        ":command_echo_1",
    ],
    jobs = 0,
)

multirun(
    name = "multirun_parallel_tagged_success",
    jobs = 0,
    tagged_commands = {
        ":command_echo_2": "echo-2",
        ":command_echo_1": "echo-1",
    },
)

command(
    name = "command_echo_1",
    arguments = [
        "command_1 a",
        "command_1 b",
    ],
    command = ":echo",
)

command(
    name = "command_echo_2",
    arguments = [
        "command_2 a",
        "command_2 b",
        "command_2 c",
    ],
    command = ":echo",
)

command(
    name = "command_echo_3",
    arguments = [
        "command_3 a",
    ],
    command = ":echo",
)

multirun(
    name = "multirun_failure",
    commands = [
        ":command_echo_2",
        ":command_exit",
        ":command_echo_3",
    ],
)

multirun(
    name = "multirun_failure_no_stop",
    commands = [
        ":command_echo_2",
        ":command_exit",
        ":command_echo_3",
    ],
    stop_on_error = False,
)

multirun(
    name = "multirun_parallel_failure",
    commands = [
        ":command_echo_2",
        ":command_sleep_and_exit_0",
        ":command_sleep_and_exit_5",
    ],
    jobs = 0,
)

multirun(
    name = "multirun_parallel_failure_no_stop",
    commands = [
        ":command_echo_2",
        ":command_sleep_and_exit_0",
        ":command_sleep_and_exit_2",
        ":command_sleep_and_exit_5",
    ],
    jobs = 0,
    stop_on_error = False,
)

command(
    name = "command_exit",
    arguments = [
        "5",
    ],
    command = ":exit",
)

sh_test(
    name = "test_multirun_parallel_sigterm",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "'$(location :run_and_kill) $(location :multirun_parallel_signal_handling) 2 SIGTERM'",
        "255",
        "$(location expected_parallel_sigterm.txt)",
    ],
    data = [
        "expected_parallel_sigterm.txt",
        ":multirun_parallel_signal_handling",
        ":run_and_kill",
    ],
)

sh_test(
    name = "test_multirun_parallel_sigint",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "'$(location :run_and_kill) $(location :multirun_parallel_signal_handling) 2 SIGINT'",
        "255",
        "$(location expected_parallel_sigint.txt)",
    ],
    data = [
        "expected_parallel_sigint.txt",
        ":multirun_parallel_signal_handling",
        ":run_and_kill",
    ],
)

sh_test(
    name = "test_multirun_sigterm",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "'$(location :run_and_kill) $(location :multirun_signal_handling) 2 SIGTERM'",
        "255",
        "$(location expected_sigterm.txt)",
    ],
    data = [
        "expected_sigterm.txt",
        ":multirun_signal_handling",
        ":run_and_kill",
    ],
)

sh_test(
    name = "test_multirun_sigint",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "'$(location :run_and_kill) $(location :multirun_signal_handling) 2 SIGINT'",
        "255",
        "$(location expected_sigint.txt)",
    ],
    data = [
        "expected_sigint.txt",
        ":multirun_signal_handling",
        ":run_and_kill",
    ],
)

sh_binary(
    name = "run_and_kill",
    srcs = ["run_and_kill.sh"],
)

sh_binary(
    name = "sleep_and_exit",
    srcs = ["sleep_and_exit.sh"],
)

multirun(
    name = "multirun_signal_handling",
    commands = [
        ":command_sleep_and_exit_0",
    ],
)

multirun(
    name = "multirun_parallel_signal_handling",
    commands = [
        ":command_sleep_and_exit_0",
    ],
    jobs = 0,
)

command(
    name = "command_sleep_and_exit_0",
    arguments = [
        "4",
        "0",
    ],
    command = ":sleep_and_exit",
)

command(
    name = "command_sleep_and_exit_2",
    arguments = [
        "2",
        "2",
    ],
    command = ":sleep_and_exit",
)

command(
    name = "command_sleep_and_exit_5",
    arguments = [
        "1",
        "5",
    ],
    command = ":sleep_and_exit",
)

sh_test(
    name = "test_multirun_generated_executables",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :multirun_generated_executable)",
        "0",
        "$(location expected_multirun_generated_executable.txt)",
    ],
    data = [
        "expected_multirun_generated_executable.txt",
        ":multirun_generated_executable",
    ],
)

multirun(
    name = "multirun_generated_executable",
    commands = [
        ":hello",
        ":command_generated_executable",
        ":command_static_executable",
        ":hello2",
    ],
)

sh_test(
    name = "test_command_generated_executable",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :command_generated_executable)",
        "0",
        "$(location expected_command_generated_executable.txt)",
    ],
    data = [
        "expected_command_generated_executable.txt",
        ":command_generated_executable",
    ],
)

command(
    name = "command_generated_executable",
    command = ":hello1",
)

sh_test(
    name = "test_command_static_executable",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :command_static_executable)",
        "0",
        "$(location expected_command_static_executable.txt)",
    ],
    data = [
        "expected_command_static_executable.txt",
        ":command_static_executable",
    ],
)

command(
    name = "command_static_executable",
    command = ":hello",
)

sh_binary(
    name = "hello",
    srcs = ["hello.sh"],
)

genrule(
    name = "hello1",
    outs = ["hello1.sh"],
    cmd = """echo '#!/usr/bin/env bash\necho "Generated hello 1!"' > $@""",
    executable = True,
)

genrule(
    name = "hello2",
    outs = ["hello2.sh"],
    cmd = """echo '#!/usr/bin/env bash\necho "Generated hello 2"' > $@""",
    executable = True,
)

sh_test(
    name = "test_multirun_data",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :multirun_cat)",
        "0",
        "$(location expected_multirun_cat.txt)",
    ],
    data = [
        "expected_multirun_cat.txt",
        ":multirun_cat",
    ],
)

multirun(
    name = "multirun_cat",
    commands = [
        ":command_cat",
    ],
)

sh_test(
    name = "test_command_data",
    size = "small",
    srcs = ["test_compare_content.sh"],
    args = [
        "$(location :command_cat)",
        "0",
        "$(location expected_command_cat.txt)",
    ],
    data = [
        "expected_command_cat.txt",
        ":command_cat",
    ],
)

command(
    name = "command_cat",
    arguments = [
        "$(location cat1.txt)",
    ],
    command = ":cat",
    data = [
        "cat1.txt",
    ],
)

sh_binary(
    name = "cat",
    srcs = ["cat.sh"],
    data = ["cat2.txt"],
)
